
import sys,os
import optparse

import pathlib
toolnameRaw = pathlib.Path(__file__).absolute().parts[-1]
toolname = toolnameRaw[2:-5]
def getArgv():
    if(sys.argv.count(toolname)):
        _thisArgInd = sys.argv.index(toolname)
        return sys.argv[_thisArgInd:]
    else: return []
toolArgv = getArgv()

def func(isHelp=False):
    if(isHelp):
        return [toolname,"... | ~ %s   "%(toolname),"=grep byFile","-h"]
    if(len(toolArgv)<1):
        return parser.print_help()
    parser = optparse.OptionParser("")
    parser.add_option("-f",  '--filepath', dest="filePath", default = "./")
    parser.add_option("-s",  '--seprator', dest="seprator", default = "\n")
    parser.add_option("-e",  '--eol', dest="eol", default = "")
    parser.add_option("-m",  '--more', dest="showMore",action="store_true", default = False)
    parser.add_option("-r",  '--reverse', dest="reverseShow",action="store_true", default = False)
    options, args = parser.parse_args()
    stringInputList = sys.stdin.read().split("\n")
    def inputFilter(x):
        eollen = len(options.eol)
        if(eollen == 0):return True;
        return x[:eollen] != options.eol
    stringInputList = list(filter( inputFilter , stringInputList ))
    
    if(os.path.isfile(options.filePath)):
        with open(options.filePath) as searchFile:
            searchList = searchFile.read().split(options.seprator)
    else: return print("file not exist , use grep <str>")
    
    searchList = list(map(list,zip( searchList,[0 for i in searchList] )))
    def find(_string):
        for searchUnit in searchList:
            if(_string.find(searchUnit[0])>=0):
                searchUnit[1]=1;return True
        else: return False
    
    print(['','un'][options.reverseShow]+'matching Inputs')
    for s in stringInputList:
        if(find(s)!=options.reverseShow):print(s)
    
    if(not options.showMore): return ;
    
    print('',['','un'][options.reverseShow]+"matched keyWord:","",sep="\n")
    for unit in searchList:
        if(unit[1]!=options.reverseShow):print(unit[0])





